package com.seecen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.pojo.OrderDetail;
import com.seecen.service.OrderDetailService;
import com.seecen.mapper.OrderDetailMapper;
import com.seecen.utils.Result;
import com.seecen.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 桃子
* @description 针对表【pet_order_detail】的数据库操作Service实现
* @createDate 2025-07-24 16:44:18
*/
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail>
    implements OrderDetailService{

    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Override
    public Result selectOrderByOId(Integer orderId) {
        // 参数校验
        if (orderId == null) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }

        // 构建查询条件
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId, orderId)
                .eq(OrderDetail::getIsDeleted, 0)  // 排除已删除记录
                .orderByDesc(OrderDetail::getCreateTime);  // 按创建时间倒序

        // 执行查询
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper);

        if (orderDetails != null && !orderDetails.isEmpty()) {
            return Result.ok(orderDetails);
        } else {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
    }

    @Override
    public Result selectOrderByUId(Integer userId) {
        // 参数校验
        if (userId == null) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }

        // 构建查询条件
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getUserId, userId)
                .eq(OrderDetail::getIsDeleted, 0)  // 排除已删除记录
                .orderByDesc(OrderDetail::getCreateTime);  // 按创建时间倒序

        // 执行查询
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper);

        if (orderDetails != null && !orderDetails.isEmpty()) {
            return Result.ok(orderDetails);
        } else {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
    }

    @Override
    public Result selectOrderByid(Integer id) {
        // 参数校验
        if (id == null) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }

        // 先查询记录是否存在
        OrderDetail orderDetail = baseMapper.selectById(id);
        if (orderDetail == null || orderDetail.getIsDeleted() == 1) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }

        // 执行逻辑删除（更新isDeleted字段为1）
        OrderDetail updateEntity = new OrderDetail();
        updateEntity.setId(orderDetail.getId());
        updateEntity.setIsDeleted(1);

        int rows = baseMapper.updateById(updateEntity);

        if (rows > 0) {
            return Result.ok("null");
        } else {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
    }

    @Override
    public Result selectAddressByUid(Long userId) {
        return null;
    }
}




